package com.wenlin.myproduction.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {

    private static final int ONE_MIN_EQUAL_SECOND = 60;
    private static final int ONE_HOUR_EQUAL_SECOND = 60 * 60;

    public static String formatHourMin(long millis) {
        int seconds = (int) millis;
        return formatHourMin(seconds);
    }

    public static String formatHourMinByMin(float min) {
        int seconds = (int) min * 60;
        return formatHourMin(seconds);
    }

    public static String formatHourMin(float millis) {
        int seconds = (int) millis;
        return formatHourMin(seconds);
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatHourMin(int seconds) {
        int leftMin = seconds / ONE_MIN_EQUAL_SECOND;
        return leftMin + "分钟";
    }

    /**
     * 农场用来通过分钟数输出格式化的小时分钟
     *
     * @param seconds
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatHourMinBySeconds(long seconds) {
        StringBuilder sb = new StringBuilder();
        long leftHour = seconds / ONE_HOUR_EQUAL_SECOND;
        if (leftHour < 10)
            sb.append("0");
        sb.append(leftHour).append(":");
        long leftMin = (seconds - leftHour * ONE_HOUR_EQUAL_SECOND) / ONE_MIN_EQUAL_SECOND;//总秒数-小时数*每小时的秒数=不足一小时的秒数，不足一小时的秒数/一分钟的秒数=分钟数
        if (leftMin < 10)
            sb.append("0");
        sb.append(leftMin).append(":");
        long leftSecond = seconds % 60;
        if (leftSecond < 10)
            sb.append("0");
        sb.append(leftSecond);
        return sb.toString();
    }

    /**
     * 用来通过秒数输出格式化的分钟秒
     *
     * @param seconds
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatMinSecondBySeconds(long seconds) {
        StringBuilder sb = new StringBuilder();
        long leftMin = seconds / ONE_MIN_EQUAL_SECOND;
        if (leftMin < 10)
            sb.append("0");
        sb.append(leftMin).append(":");
        long leftSecond = seconds % 60;
        if (leftSecond < 10)
            sb.append("0");
        sb.append(leftSecond);
        return sb.toString();
    }

    /**
     * 用来通过秒数输出格式化的分钟秒
     *
     * @param seconds
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatMinSecondBySecondsWithUnit(long seconds) {
        long leftMin = seconds / ONE_MIN_EQUAL_SECOND;
        long leftSecond = seconds % ONE_MIN_EQUAL_SECOND;
        StringBuilder sb = new StringBuilder();
        if (leftMin > 0)
            sb.append(leftMin).append("分");
        sb.append(leftSecond).append("秒");
        return sb.toString();
    }

    @SuppressLint("SimpleDateFormat")
    public static String getDateToday() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getTodayYYYYMMDD() {
        return new SimpleDateFormat("yyyyMMdd").format(new Date(System.currentTimeMillis()));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getTodayDD() {
        return new SimpleDateFormat("dd").format(new Date(System.currentTimeMillis()));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getYesterdayDD() {
        return new SimpleDateFormat("dd").format(new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000));//减去一天的毫秒数
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatDate(long time) {
        if ((time + "").length() == 10) {
            time *= 1000;
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
    }

    //获取今天零点时间
    public static long getTodayDayZero() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    //获取第二天时间
    public static long getNextDayTime() {
        Calendar cal = Calendar.getInstance();
        //日期加1
        cal.add(Calendar.DAY_OF_YEAR, 1);
        //时间设定到8点整
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }


    public static String getTime(long time, String pattern) {
        String timeStr = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            timeStr = format.format(new Date(time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeStr;
    }

    public static String getTimeEnglish(long time, String pattern) {
        String timeStr = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
            timeStr = format.format(new Date(time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeStr;
    }

    public static String dateToWeek(long time) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTimeInMillis(time);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    //计算播放时间
    public static String calculateTime(int time) {
        int hour = time / (60 * 60);
        int minute = (time / 60) % 60;
        int second = time % 60;
        String hourStr = hour > 0 ? hour >= 10 ? hour + ":" : "0" + hour + ":" : "";
        String minuteStr = minute >= 10 ? minute + ":" : "0" + minute + ":";
        String secondStr = second >= 10 ? second + "" : "0" + second;
        return hourStr + minuteStr + secondStr;
    }

    /**
     * 将秒数转换为日时分秒，
     *
     * @param second
     * @return
     */
    public static String secondToTime(long second) {
        long days = second / 86400;            //转换天数
        second = second % 86400;            //剩余秒数
        long hours = second / 3600;            //转换小时
        second = second % 3600;                //剩余秒数
        long minutes = second / 60;            //转换分钟
        second = second % 60;                //剩余秒数
        String result = "";
        if (days > 0) {
            result += days + "天";
        }
        if (hours > 0) {
            result += hours + "小时";
        }
        if (minutes > 0) {
            result += minutes + "分";
        }
        if (second > 0) {
            result += second + "秒";
        }
//        if(days>0){
//            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
//        }else{
//            return hours + "小时" + minutes + "分" + second + "秒";
//        }
        return result;
    }

    public static String getYearAndMonth(String dateStr) {
        if (TextUtils.isEmpty(dateStr)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        try {
            Date date = sdf.parse(dateStr);
            Calendar now = Calendar.getInstance();
            now.setTime(date);
            int year = now.get(Calendar.YEAR);
            int month = now.get(Calendar.MONTH) + 1; // 0-based!
            return getEglishMonth(month) + "\n" + year;
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String getEglishMonth(int month) {
        String str = "";
        switch (month) {
            case 1:
                str = "Jan";
                break;
            case 2:
                str = "Feb";
                break;
            case 3:
                str = "Mar";
                break;
            case 4:
                str = "Apr";
                break;
            case 5:
                str = "May";
                break;
            case 6:
                str = "Jun";
                break;
            case 7:
                str = "Jul";
                break;
            case 8:
                str = "Aug";
                break;
            case 9:
                str = "Sep";
                break;
            case 10:
                str = "Oct";
                break;
            case 11:
                str = "Nov";
                break;
            case 12:
                str = "Dec";
                break;
        }
        return str;
    }

    public static int getDay(String dateStr) {
        if (TextUtils.isEmpty(dateStr)) {
            return 0;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        try {
            Date date = sdf.parse(dateStr);
            Calendar now = Calendar.getInstance();
            now.setTime(date);
            int day = now.get(Calendar.DAY_OF_MONTH);
            return day;
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return 0;
    }

    public static boolean isSampleDay(String date) {
        if (TextUtils.isEmpty(date)) {
            return false;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");//设置日期格式
        return TextUtils.equals(date, df.format(new Date()));
    }

    public static String getTodayFormatYYYYMMDD() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }

    public static String getNowFormatYYYYMMDDHHmmss() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }

    public static boolean isToDayFormatYYYYMMDD(String date) {
        if (TextUtils.isEmpty(date)) {
            return false;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        return TextUtils.equals(date, df.format(new Date()));
    }

    public static String getCurrentDateFormatYYYYMMDD() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        return df.format(new Date());
    }

    public static String getCurrentDateFormatYYYY_MM_DD() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());
    }

    /**
     * 获取当前的年月
     *
     * @return 所求
     */
    public static String getCurrentYearMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }

    /**
     * 获取当前的日期化字符串
     *
     * @return 所求
     */
    public static String getTodayMonthDay() {
        String str = "";
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        Date date = new Date(System.currentTimeMillis());
        str = format.format(date);
        return str;
    }

    public static boolean isNumberOne() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int day = c.get(Calendar.DAY_OF_MONTH);
        System.out.println(day);
        return day == 1;
    }

    public static String getDateByDay(int day) {
        SimpleDateFormat formatTemp = new SimpleDateFormat("yyyyMMdd");
        Calendar cale = Calendar.getInstance();
        cale.setTime(new Date());
        cale.set(Calendar.DAY_OF_MONTH, day);
        String firstday = formatTemp.format(cale.getTime());
        return firstday;
    }

    /**
     * 判断某个日期是否在两个日期范围之外
     *
     * @param startStr
     * @param nowStr
     * @param endStr
     * @return
     */
    public static boolean isEffectiveDate(String startStr, String endStr, String nowStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date startDate = null;
        Date nowDate = null;
        Date endDate = null;
        try {
            startDate = simpleDateFormat.parse(startStr);
            endDate = simpleDateFormat.parse(endStr);
            nowDate = simpleDateFormat.parse(nowStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nowDate.getTime() >= startDate.getTime() && nowDate.getTime() <= endDate.getTime();
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 获取当前是一星期的第几天
     * @return 所求
     */
    public static int getTodayOfWeek(){
        int [] weekDays = {6,0,1,2,3,4,5};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(System.currentTimeMillis()));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(String beginTime, String endTime) {
        if (TextUtils.isEmpty(beginTime) || TextUtils.isEmpty(endTime)) {
            return true;
        }
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");// 设置日期格式
        Date beginDate = null;
        Date currentDate = null;
        Date endDate = null;
        try {
            beginDate = df.parse(beginTime);
            currentDate = df.parse(df.format(new Date()));
            endDate = df.parse(endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(currentDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        if (currentCalendar.after(beginCalendar) && currentCalendar.before(endCalendar)) {
            return true;
        } else {
            return false;
        }
    }


    public static void main(String[] args) {
//        System.out.println(getWeekOfDate(new Date()));
        Boolean flag = belongCalendar("00:00", "23:00");
    }

    /**
     * 返回某年某月的天数
     * @param year 年
     * @param month 月
     * @return 所求
     */
    public static  int getDaysInMonth(int year, int month){
        int days;
        int febDay = 28;
        if(isLeap(year)){
            febDay = 29;
        }
        switch (month){
            case 2:
                days = febDay;
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            default:
                days = 0;
        }
        return days;
    }

    /**
     * 是否是闰年
     * @param year 年
     * @return 所求
     */
    public static boolean isLeap(int year){//能被100整除且能被400整除才是闰年，剩余的能被四整除不能被100整除。四年一闰，百年不闰，四百年再闰。 例如，2000年是闰年，1900年则是平年。
        return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
    }
}
